﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TransCode.Entitys;
using System.IO;

namespace TransCodeTask
{
    public class TargetTask
    {
        Dictionary<string, string> frameDictionary = new Dictionary<string, string>();

        public List<Target> TargetInfoList = new List<Target>();

        public void TargetInfoBuilder(string fileName, string SourceFilePath, string TargetFilePath, int AddTime)
        {
            string dir = fileName.Substring(0, fileName.IndexOf('.'));
            string filePath = SourceFilePath + dir + ".avi.xml";
            XElement doc = XElement.Load(filePath);

            int ObjectNumber = int.Parse(doc.Descendants("ObjectNumber").FirstOrDefault().Value);

            for (int i = 1; i <= ObjectNumber; i++)
            {
                Target info = new Target();
                string imagePath = new DirectoryInfo(SourceFilePath + i.ToString()).GetFiles().First().FullName;
                string imageName = dir + "_" + i + ".jpg";
                string targetPath = TargetFilePath + "\\" + imageName;
                File.Move(imagePath + "1.jpg", targetPath);
                info.ImagePath = TargetFilePath;
                info.ImageName = imageName;
                info.Id = Guid.NewGuid().ToString();
                string FrameBegin = doc.Element("T" + i.ToString()).Element("FrameBegin").Value;
                string FrameLast = doc.Element("T" + i.ToString()).Element("FrameLast").Value;

                if (frameDictionary.ContainsKey(FrameBegin))
                {
                    info.BeginTime = new TimeSpan(0, 0, int.Parse(frameDictionary[FrameBegin]) / 25 + AddTime);
                }
                if (frameDictionary.ContainsKey(FrameLast))
                {
                    info.EndTime = new TimeSpan(0, 0, int.Parse(frameDictionary[FrameLast]) / 25 + AddTime);
                }

                TargetInfoList.Add(info);
            }
        }

        //FrameDictionaryBuilder(direct.FullName + @"\configs\CorrFrameNumber.xml");
        public void FrameDictionaryBuilder(string fileName, string SourceFilePath)
        {
            string dir = fileName.Substring(0, fileName.IndexOf('.'));

            XElement doc = XElement.Load(SourceFilePath + dir + "\\cutimage\\CorrFrameNumber.xml");

            var OriginalFrameNumbers = doc.Element("OriginalFrameNumber").Value.Replace("\n ", "").Split(new char[] { ' ' });
            var CutFrameNumbers = doc.Element("CutFrameNumber").Value.Replace("\n ", "").Split(new char[] { ' ' });

            int frameCount = OriginalFrameNumbers.Length > CutFrameNumbers.Length ? CutFrameNumbers.Length : OriginalFrameNumbers.Length;

            for (int i = 0; i < frameCount; i++)
            {
                if (OriginalFrameNumbers[i].Length > 0 && OriginalFrameNumbers[i] != "0")
                    frameDictionary.Add(OriginalFrameNumbers[i], CutFrameNumbers[i]);
            }

        }

    }
}
